import queue
import threading
import time
import random

# 创建一个队列
q = queue.Queue()


def worker():
    while True:
        item = q.get()
        print(f"working on {item}")
        time.sleep(random.randint(1, 3))
        q.task_done()
        print(f"finished {item} ---------")


# 创建一个线程
threading.Thread(target=worker, daemon=True).start()

for item in range(5):
    q.put(item)

# 等待队列中的所有任务完成
q.join()
print("All tasks are done")
